#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=25;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
int n,a[N],q[N],sum[N],cnt[N],g[N][N],ans;
bool vis[N];
void dfs(int dep){
	if(dep==n+1){
		int res=0;
		for(int i=1;i<=n;i++) sum[i]=a[i];
		for(int i=1;i<=n;i++){
			int u=q[i];
			res=res+sum[u];
			for(int j=1;j<=cnt[u];j++)
				sum[g[u][j]]+=sum[u];
		}
		ans=max(ans,res);
		return;
	}
	for(int i=1;i<=n;i++)
		if(!vis[i]) vis[i]=1,q[dep]=i,dfs(dep+1),vis[i]=0;
}
signed main(){
	freopen("light.in","r",stdin);
	freopen("light.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++) a[i]=read();
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		g[u][++cnt[u]]=v;
		g[v][++cnt[v]]=u;
	}
	dfs(1);
	printf("%lld",ans);
	return 0;
}
